package timing.ukulele.portal.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import timing.ukulele.portal.mapper.SystemMapper;
import timing.ukulele.common.portal.SystemData;
import timing.ukulele.persistent.service.BaseService;
import timing.ukulele.portal.persistent.System;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SystemService extends BaseService<SystemMapper, System> {

    /**
     * 获取分页
     *
     * @param map 查询的参数条件
     * @param current  当前也页，从1开始
     * @param size     每页大小
     * @return 模板数据
     */
    public IPage<System> getPage(Map<String, Object> map, int current, int size) {
        Page<System> page = new Page<>(current, size);
        LambdaQueryWrapper<System> qw = new LambdaQueryWrapper<>();
        if (map.get("name") != null) {
            qw.like(System::getName, map.get("name"));
        }
        if (map.get("id") != null) {
            qw.eq(System::getId, map.get("id"));
        }
        qw.orderByDesc(System::getId);
        IPage<System> iPage = getBaseMapper().selectPage(page, qw);
        return iPage;
    }

    public List<System> findAllWithPermission(String idList){
        LambdaQueryWrapper<System> qw = new LambdaQueryWrapper<>();
        qw.orderByDesc(System::getId);
        List<Long> systemIds = null;
        if(!"".equals(idList)){
            String[] split = idList.split(",");
            if(!"0".equals(split[0])){
                systemIds = Arrays.asList(split).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
                qw.in(System::getId, systemIds);
            }
        }
        return getBaseMapper().selectList(qw);
    }

    public List<Long> getSystemIdByUserId(Long userId){
        return getBaseMapper().getSystemIdByUserId(userId);
    }

    public List<SystemData> getLikeSystemName(String systemName){
        LambdaQueryWrapper<System> qw = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(systemName)) {
            qw.like(System::getName, systemName);
        }
        List<System> systems = getBaseMapper().selectList(qw);
        List<SystemData> systemDatas = new ArrayList<>();
        for (System system : systems) {
            SystemData systemData = new SystemData();
            BeanUtils.copyProperties(system, systemData);
            systemDatas.add(systemData);
        }
        return systemDatas;
    }

    public System getByCode(String code){
        LambdaQueryWrapper<System> qw = new LambdaQueryWrapper<>();
        qw.orderByDesc(System::getId);
        qw.eq(System::getCode,code);
        return getBaseMapper().selectOne(qw);
    }


}
